package com.tuo51.evaluate.controller;


import com.tuo51.core.domain.staffs.Staff;
import com.tuo51.core.service.StaffService;
import com.tuo51.evaluate.domain.news.Article;
import com.tuo51.evaluate.service.article.ArticleService;
import com.tuo51.foudation.model.MemVerificationCache;
import com.tuo51.foudation.model.StatusResponse;
import com.tuo51.foudation.query.base.PagerQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.w3c.dom.Document;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;

import static com.tuo51.foudation.constant.RequestConstant.APP_CHANNEL;
import static com.tuo51.foudation.constant.RequestConstant.TICKET;

@RestController
@RequestMapping("/eapi/news")
@Api(value = "/eapi/news", tags = "测评 - 运营位，新闻列表")
public class NewsController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private StaffService staffService;

    @Autowired
    private MemVerificationCache cache;

    @GetMapping(value = "/article/{articleId}",produces = "text/html;charset=utf-8")
    @ApiOperation("查看文章")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "articleId", value = "文章id", required = true),
            @ApiImplicitParam(name = "ticket", value = "票据", paramType = "header", required = false)

    })
    public String articleDetail(@RequestHeader(value = "ticket", required = false) String ticket,
                                  @PathVariable("articleId") String articleId) {
        Article article = articleService.articleDetail(articleId);

        return article.getContent();
    }

    @GetMapping(value = "/list")
    public StatusResponse newsList(@RequestHeader(TICKET) String ticket,
                                   @RequestHeader(value = APP_CHANNEL, required = false) String channel,
                                   PagerQuery pagerQuery){
        if (pagerQuery.getPageSize() <= 0){
            pagerQuery.setPageSize(10);
        }

        Staff staff = cache.getStaff(ticket);

        return StatusResponse.success(articleService.homePageNews(staffService.isYearsVip(staff), channel, pagerQuery));
    }
}
